package com.infore.operation.controller;

/**
 * 
 */
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.infore.operation.entity.AttendanceInfo;
import com.infore.operation.entity.AttendanceRule;
import com.infore.operation.entity.Inspect;
import com.infore.operation.entity.WorkType;
import com.infore.operation.entity.reportDetil;
import com.infore.operation.service.PunchtheService;
import com.infore.operation.utils.AbstractJsonObject;
import com.infore.operation.utils.FormatUtil;
import com.infore.operation.utils.ListObject;
import com.infore.operation.utils.StatusCode;

@RestController
@RequestMapping("/servlet/attendance")
public class PunchtheController {
    @Autowired
    private PunchtheService punchtheServiceImpl;

    /**
     * 打卡记录的提交
     * 
     * @param attendanceTime
     * @param attendanceType
     * @param lat
     * @param lnt
     * @param address
     * @param createPeopleId
     * @param createPeopleName
     * @return
     */
    @RequestMapping(value = "/punchThecClock", method = RequestMethod.POST)
    public AbstractJsonObject saveAttemdance(
            @RequestParam(value = "attendanceId", required = false) String attendanceId,
            @RequestParam String attendanceTime, @RequestParam Integer attendanceType, @RequestParam Integer supplement,
            @RequestParam(value = "latitude", required = false, defaultValue = "0") double latitude,
            @RequestParam(value = "longitude", required = false, defaultValue = "0") double longitude,
            @RequestParam(value = "address", required = false) String address, @RequestParam Integer userId,
            @RequestParam String remark, @RequestParam(value = "file", required = false) MultipartFile[] file) {
        AttendanceInfo info = new AttendanceInfo();
        info.setAttendanceTime(attendanceTime);
        info.setAttendanceType(attendanceType);
        info.setLatitude(latitude);
        info.setLongitude(longitude);
        info.setAddress(address);
        info.setCreatePeopleId(userId);
        info.setAttendanceId(attendanceId);
        info.setRemark(remark);
        String code = punchtheServiceImpl.saveAttendace(info, file, supplement);
        if (code == null || code .equals("") ) {
            return FormatUtil.queryAbstractJsonObject("提交成功", StatusCode.CODE_SUCCESS);
        }
        return FormatUtil.queryAbstractJsonObject(code, StatusCode.CODE_ERROR);
    }

    /**
     * 新增日报信息
     * 
     * @param targetDate
     * @param content
     * @param imges
     * @param workTypeId
     * @param linkId
     * @param userId
     * @return
     */
    @RequestMapping(value = "/report", method = RequestMethod.POST)
    public AbstractJsonObject saveReporting(@RequestParam String targetDate, @RequestParam String content,
            @RequestParam(value = "file", required = false) MultipartFile[] file, @RequestParam String workTypeId,
            @RequestParam(value = "linkId", required = false) String linkId, @RequestParam Integer userId) {
        Map<String, Object> reportMap = new HashMap<String, Object>();
        reportMap.put("targetDate", targetDate);
        reportMap.put("content", content);
        reportMap.put("workTypeId", workTypeId);
        reportMap.put("userId", userId);
        reportMap.put("photo", 0);
        String result = punchtheServiceImpl.saveReport(reportMap, file, linkId);
        if (result.equals("")) {
            return FormatUtil.queryAbstractJsonObject("提交成功", StatusCode.CODE_SUCCESS);
        }
        return FormatUtil.queryAbstractJsonObject(result, StatusCode.CODE_ERROR);
    }

    // 查询工作类型
    @RequestMapping(value = "/workTypes", method = RequestMethod.GET)
    public ListObject queryworkTypes() {
        List<WorkType> workTypes = punchtheServiceImpl.queryworkTypes();
        if (workTypes == null) {
            return FormatUtil.queryListObject("", "查询失败");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", workTypes);
        return FormatUtil.queryListObject(map, "查询成功");
    }

    /**
     * 查询巡检记录
     * 
     * @param userId
     * @param date
     * @return
     */
    @RequestMapping(value = "/inspectionRecords", method = RequestMethod.GET)
    public ListObject queryInspectionRecords(@RequestParam Integer userId, @RequestParam String date) {
        List<Map<String, Object>> inspection = punchtheServiceImpl.queryinspectionRecords(userId, date);
        if (inspection == null) {
            return FormatUtil.queryListObject("", "查询失败");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", inspection);
        return FormatUtil.queryListObject(map, "查询成功");
    }

    /**
     * 查询日报列表
     * 
     * @param date
     * @param range
     * @param userId
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/reports", method = RequestMethod.GET)
    public ListObject queryReports(@RequestParam(value="date",required = false) String date, @RequestParam Integer range, @RequestParam Integer userId,
            @RequestParam Integer pageNum) {
        List<reportDetil> reports = punchtheServiceImpl.queryReports(date, range, userId, pageNum);
        if (reports == null) {
            return FormatUtil.queryListObject("", "查询失败");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", reports);
        return FormatUtil.queryListObject(map, "查询成功");
    }

    /**
     * 
     * @param userId
     * @param startTime
     * @param endTime
     * @param range
     * @return
     */
    @RequestMapping(value = "/reportTypeStatistics", method = RequestMethod.GET)
    public ListObject queryReportTypeStatistics(@RequestParam Integer userId, @RequestParam String startTime,
            @RequestParam String endTime, @RequestParam Integer range) {
        Map<String, Object> reportSubmissionStateStatistics = punchtheServiceImpl.queryReportTypeStatistics(startTime,
                endTime, userId, range);
        if (reportSubmissionStateStatistics == null) {
            return FormatUtil.queryListObject("", "查询失败");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("bean", reportSubmissionStateStatistics);
        return FormatUtil.queryListObject(map, "查询成功");
    }

    /**
     * 查询日报月度提交状态统计
     * 
     * @param userId
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping(value = "/reportSubmissionStateStatistics", method = RequestMethod.GET)
    public ListObject queryReportSubmissionStateStatistics(@RequestParam Integer userId, @RequestParam String startTime,
            @RequestParam String endTime) {
        List<Map<String, Object>> reportSubmissionStateStatistics = punchtheServiceImpl
                .queryReportSubmissionStateStatistics(userId, startTime, endTime);
        if (reportSubmissionStateStatistics == null) {
            return FormatUtil.queryListObject("", "查询失败");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", reportSubmissionStateStatistics);
        return FormatUtil.queryListObject(map, "查询成功");
    }

    /**
     * 查询下属日报统计列表
     * 
     * @param date
     * @param userId
     */
    @RequestMapping(value = "/powerReportInfo", method = RequestMethod.GET)
    public ListObject queryPowerReportInfo(@RequestParam String date, @RequestParam Integer userId) {
        List<Map<String, Object>> queryPowerReportInfo = punchtheServiceImpl.queryPowerReportInfo(date, userId);
        if (queryPowerReportInfo == null) {
            return FormatUtil.queryListObject("", "查询失败");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", queryPowerReportInfo);
        return FormatUtil.queryListObject(map, "查询成功");
    }

    /**
     * 删除
     * 
     * @param reportId
     * @return
     */
    @RequestMapping(value = "/deleteReport", method = RequestMethod.POST)
    public AbstractJsonObject deleteReport(@RequestParam String reportId) {
        Integer num = punchtheServiceImpl.deleteReportId(reportId);
        if (num != null && num != 0) {
            return FormatUtil.queryAbstractJsonObject("删除成功", StatusCode.CODE_SUCCESS);
        }
        return FormatUtil.queryAbstractJsonObject("删除失败", StatusCode.CODE_ERROR);
    }

    /**
     * 打卡规则的获取
     * 
     * @param attendanceTime
     * @param attendanceType
     * @param lat
     * @param lnt
     * @param address
     * @param createPeopleId
     * @param createPeopleName
     * @return
     */
    @RequestMapping(value = "/rule", method = RequestMethod.GET)
    public ListObject queryRule(@RequestParam Integer userId) {
        AttendanceRule rule = punchtheServiceImpl.queryRule(userId);
        if (rule == null) {
            return FormatUtil.error("删除失败");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("bean", rule);
        return FormatUtil.saveListObject(map, "查询成功", StatusCode.CODE_SUCCESS);

    }

    /**
     * 当天打卡记录
     * 
     * @param userId
     * @param date
     * @return
     */
    @RequestMapping(value = "/dayAttendanceRecords", method = RequestMethod.GET)
    public ListObject queryDayAttendanceRecords(@RequestParam Integer userId, @RequestParam String date) {
        List<AttendanceInfo> info = punchtheServiceImpl.queryDayAttendanceRecords(userId, date);
        if (info == null) {
            return FormatUtil.error("查询失败");
        }
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("beanList", info);
        return FormatUtil.queryListObject(map, "查询成功");
    }

    /**
     * 查询月度打卡汇总统计
     */
    @RequestMapping(value = "/monthTabulateStatistics", method = RequestMethod.GET)
    public ListObject queryMonthTabulateStatistics(@RequestParam(value = "userId") Integer userId,
            @RequestParam(value = "startDate") String startDate, @RequestParam(value = "endDate") String endDate) {
        Map<String, Object> map = punchtheServiceImpl.queryMonthTabulateStatistics(userId, startDate, endDate);
        if (map == null) {
            return FormatUtil.error("查询失败");
        }
        Map<String, Object> info = new HashMap<String, Object>();

        info.put("bean", map);
        return FormatUtil.queryListObject(info, "查询成功");
    }

    /*
     * 查询月度打卡状态统计
     */
    @RequestMapping(value = "/monthStatusStatistics", method = RequestMethod.GET)
    public ListObject queryMonthStatusStatistics(@RequestParam Integer userId, @RequestParam String startDate,
            @RequestParam String endDate) {
        List<Map<String, Object>> list = punchtheServiceImpl.queryMonthStatusStatistics(userId, startDate, endDate);
        if (list == null) {
            return FormatUtil.queryListObject("", "查询失败");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", list);
        return FormatUtil.queryListObject(map, "查询成功");

    }

    // 获取月度下属打卡汇总统计
    @RequestMapping(value = "/monthUnderlingTabulateStatistics", method = RequestMethod.GET)
    public ListObject queryMonthUnderlingTabulateStatistics(@RequestParam Integer userId,
            @RequestParam String startDate, @RequestParam String endDate, @RequestParam Integer pageNum) {
        List<Map<String, Object>> list = punchtheServiceImpl.queryMonthUnderlingTabulateStatistics(userId, startDate,
                endDate, pageNum);
        if (list == null) {
            return FormatUtil.queryListObject("", "查询失败");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", list);
        return FormatUtil.queryListObject(map, "查询成功");

    }
}
